package com.tiemei.demo.beanutils;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.PropertyUtils;

public class SetBeanProperty {
	public static void main(String[] args) {
        Track track = new Track();
 
        try {
        	/*
        	 *  set value
        	 */
            PropertyUtils.setProperty(track, "id", Long.valueOf(10));
            PropertyUtils.setProperty(track, "title", "Hey Jude");
            System.out.println("Track :\r\n" + track);
            
            /*
             * 一个map类型field中一个值
             */
            Recording re = new Recording();
            PropertyUtils.setMappedProperty(re, "mapTracks", "T1", new Track());
            System.out.println("New Track Numbers: " + re.getMapTracks().size());
            
            /*
             * set indexed value
             */
            re.getTracks().add(new Track()); // attention!
            PropertyUtils.setIndexedProperty(re, "tracks[0]", track);
            System.out.println(re.getTracks().get(0));
            
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
 
    }
}
